package com.flyme.module.file.provider.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysUserRemoteService;
import com.flyme.module.file.client.api.SysCourseRemoteService;
import com.flyme.module.file.client.entity.SysCataLog;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.provider.service.SysCataLogService;
import com.flyme.module.file.provider.service.SysCourseService;
import com.flyme.module.file.provider.service.SysCourseTypeService;
import com.flyme.module.file.provider.service.SysIndextagService;
import com.flyme.module.file.provider.service.impl.SysCataLogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "课程管理")
@RestController
public class SysCourseController extends BaseController<SysCourseService, SysCourse> implements SysCourseRemoteService {

    @Autowired
    private SysIndextagService indextagService;
    @Autowired
    private SysCourseTypeService courseTypeService;
    @Autowired
    private SysUserRemoteService userRemoteService;
    @Autowired
    private SysCataLogService cataLogService;

    /**
     * 课程列表
     * 下载秘钥:099069B471654227A895AF9479D41a
     *
     * @return
     */
    @ApiOperation(value = "获取课程分页列表", notes = "获取课程分页列表")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysCourse course) {
        String courseTypeId = course.getCourseTypeId();
        List<String> courseTypeIds = courseTypeService.selectTypeByType(courseTypeId);
        courseTypeIds.add(courseTypeId);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("c.id,courseTitle,courseTypeId,indextagId,courseImage,isFree,courseContent,courseStatus,c.sortOrder," +
            "recommend,coursePrice,c.createTime,courseType.courseTypeName,user.userName,c.scoreCourse," +
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        cq.in("c.courseTypeId", courseTypeIds);
        cq.like("courseTitle", course.getCourseTitle());
        cq.eq("courseStatus", course.getCourseStatus());
        cq.eq("indextagId", course.getIndextagId());
        cq.eq("isContinue", 0);
        cq.createAlias("courseType", "courseType");
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }


    @ApiOperation(value = "添加课程", notes = "添加课程")
    @Override
    public ResultBody addCourse(@ModelAttribute(value = "course") SysCourse course) {
        course.setIsContinue(0);
        course.setRecordNum(0);
        boolean result = bizService.addCourse(course);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "通过courseId查询课程")
    @Override
    public ResultBody get(@PathVariable String courseId) {
        Map<String, Object> course = bizService.getMap(q().eq("id", courseId));
        List<Map<String, Object>> users = userRemoteService.getChosed("UT_LECTURER", course.get("lecturerId").toString()).getData();
        course.put("users", users);
        return ResultBody.success().setData(course);
    }

    @ApiOperation(value = "编辑课程", notes = "编辑课程")
    @Override
    public ResultBody edit(SysCourse course) {
        boolean result = bizService.updateCourse(course);
        //课程为免费课程时，查询该课程目录下是否有收费目录，有的话都改为免费
        if(result && course.getIsFree().equals(0)){
            List<SysCataLog> list = cataLogService.selectByCouse(course.getId());
            if(list.size()>0){
                cataLogService.updateFree(course.getId());
            }
        }
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除课程", notes = "删除课程")
    @Override
    public ResultBody delByIds(@RequestParam("ids") String[] ids) {
        return bizService.delByIds(ids);
    }

    /**
     * 更新课程推荐状态
     *
     * @param courseId  课程ID
     * @param recommend 是否推荐
     * @return
     */
    @ApiOperation(value = "更新课程推荐状态", notes = "更新课程推荐状态")
    @Override
    public ResultBody setRecommend(@RequestParam("courseId") Long courseId, @RequestParam("recommend") Integer recommend) {
        boolean result = bizService.update(new UpdateWrapper<SysCourse>().set("recommend", recommend).eq("id", courseId));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 更新课程上架状态
     *
     * @param courseId     课程ID
     * @param courseStatus 是否上架
     * @return
     */
    @ApiOperation(value = "更新课程推荐状态", notes = "更新课程推荐状态")
    @Override
    public ResultBody setCourseStatus(@RequestParam("courseId") Long courseId, @RequestParam("courseStatus") Integer courseStatus) {
        boolean result = bizService.update(new UpdateWrapper<SysCourse>().set("courseStatus", courseStatus).eq("id", courseId));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 查询课程所属模块
     */
    @ApiOperation(value = "查询课程所属模块", notes = "查询课程所属模块")
    @Override
    public ResultBody selectIndexTag() {
        List indexTagList = indextagService.list();
        return ResultBody.ok("设置成功").setData(indexTagList);
    }

    /**
     * 更新课程模块
     *
     * @param id         课程ID
     * @param indextagId 模块ID
     * @return
     */
    @ApiOperation(value = "更新课程推荐状态", notes = "更新课程推荐状态")
    @Override
    public ResultBody setIndexTagId(@RequestParam("id") Long id, @RequestParam("indextagId") Long indextagId) {
        boolean result = bizService.update(new UpdateWrapper<SysCourse>().set("indextagId", indextagId).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }

    @ApiOperation(value = "获取课程分页列表", notes = "获取课程分页列表")
    @Override
    public ResultBody courseList(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysCourse course) {
        String courseTypeId = course.getCourseTypeId();
        List<String> courseTypeIds = courseTypeService.selectTypeByType(courseTypeId);
        courseTypeIds.add(courseTypeId);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("c.id,courseTitle,courseTypeId,indextagId,courseImage,isFree,courseContent,courseStatus,recommend,coursePrice,c.createTime,courseType.courseTypeName,user.userName," +
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        cq.in("c.courseTypeId", courseTypeIds);
        if(ObjectUtils.isEmpty(course.getIsContinue())){
            cq.eq("isContinue", 0);
        }else{
            cq.eq("isContinue", course.getIsContinue());
        }
        cq.like("courseTitle", course.getCourseTitle());
        cq.eq("courseStatus", course.getCourseStatus());
        cq.eq("indextagId", course.getIndextagId());
        cq.last("AND (c.isFree = 0 or isVipFree = 0)");
        cq.createAlias("courseType", "courseType");
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加继续教育课程", notes = "添加继续教育课程")
    @Override
    public ResultBody addContinueCourse(@ModelAttribute(value = "course") SysCourse course) {
        course.setIsContinue(1);
        course.setRecordNum(0);
        boolean result = bizService.addCourse(course);
        return ResultBody.success(result);
    }


    @ApiOperation(value = "获取继续教育课程分页列表", notes = "获取继续教育课程分页列表")
    @Override
    public ResultBody courseContinueList(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysCourse course) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("c.*,user.userName," +
                "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        cq.like("courseTitle", course.getCourseTitle());
        if(ObjectUtils.isNotEmpty(course.getIsContinue())){
            cq.eq("isContinue", course.getIsContinue());
        }else{
            cq.eq("isContinue", 1);
        }
        if(ObjectUtils.isNotEmpty(course.getCourseStatus())){
            cq.eq("courseStatus", course.getCourseStatus());
        }
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }


    @ApiOperation(value = "添加试听课程", notes = "添加试听课程")
    @Override
    public ResultBody addAuditionCourse(@ModelAttribute(value = "course") SysCourse course) {
        course.setIsContinue(2);
        course.setRecordNum(0);
        boolean result = bizService.addCourse(course);
        return ResultBody.success(result);
    }


    @ApiOperation(value = "获取试听课程分页列表", notes = "获取试听课程分页列表")
    @Override
    public ResultBody courseAuditionList(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysCourse course) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("c.*,user.userName," +
                "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        cq.like("courseTitle", course.getCourseTitle());
        if(ObjectUtils.isNotEmpty(course.getIsContinue())){
            cq.eq("isContinue", course.getIsContinue());
        }else{
            cq.eq("isContinue", 2);
        }
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }
}

